﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Twi.COMMON.Core;

namespace Twi.DataAccess.Common
{
    /*
    * Copyright: ©2014 踏影软件开发工作室【Tyingsoft、Twilight、Twifly】版权所有
    * Author: xuzhihong
    * Create date: 2014-05-26
    * Description: TableMapping辅助类
    * 
    */
    public class TableMappingHelper
    {
        /// <summary>
        /// 根据Code获取TableMapping
        /// </summary>
        /// <param name="tableMappingFileFullPath"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetByCode(string tableMappingFileFullPath, string code, DbProviderType dbProviderType = DbProviderType.Oracle)
        {
            SQLMapCommandInfo commandInfo = new SQLMapCommandInfo();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(tableMappingFileFullPath);
            XmlElement root = xmlDoc.DocumentElement;
            XmlNode node = default(XmlNode);
            node = root.SelectSingleNode("//TableMapping[@Code='" + code + "']");
            if (node == null)
            {
                throw new Exception("TableMapping中未找到Code为：【" + code + "】的配置");
            }
            string strSql = "";
            if (node.ChildNodes.Count == 1) //通用SQL
            {
                strSql = node.InnerText;
            }
            else //非通用SQL
            {
                strSql = node.SelectSingleNode(TwiEnumHelper.GetName(dbProviderType == DbProviderType.OracleDataAccess ? DbProviderType.Oracle : dbProviderType)).InnerText;
            }            
            return " (" + strSql + ") TM ";
        }
    }
}
